<template>
  <div class="login-container">
    <div class="nav-bar">
      <div class="nav-left">
        <div class="logo">
          <img src="@/assets/logo.png" alt="灵溪">
        </div>
      </div>
    </div>
    <div class="content">
      <div class="login-box">
        <div class="login-form">
          <h2 v-if="isLogin">登录</h2>
          <h2 v-else>注册</h2>
          <p class="subtitle">使用您的手机{{ isLogin ? '登录' : '注册' }}</p>

          <el-form v-if="!isLogin" :model="registerForm" :rules="rules" ref="registerFormRef" :disabled="loading">
            <el-form-item prop="nickname">
              <el-input
                  v-model="registerForm.nickname"
                  placeholder="用户名"
                  type="text"
              ></el-input>
            </el-form-item>

            <el-form-item prop="phone">
              <el-input
                  v-model="registerForm.phone"
                  placeholder="手机"
                  type="phone"
              ></el-input>
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                  v-model="registerForm.password"
                  placeholder="密码"
                  type="password"
                  show-password
              ></el-input>
            </el-form-item>

            <el-button type="primary" :loading="loading" class="login-button" @click="isLogin ? handleLogin() : handleRegister()">{{ isLogin ? '登录' : '注册' }}</el-button>
            <div>
              <el-button plain class="toggle-button" @click="toggleForm">{{ isLogin ? '没有账户？注册' : '已有账户？登录' }}</el-button>
            </div>
          </el-form>
          <el-form v-if="isLogin" :model="loginForm" :rules="rules" ref="loginFormRef" :disabled="loading">
            <el-form-item prop="phone">
              <el-input
                  v-model="loginForm.phone"
                  placeholder="手机"
                  type="phone"
              ></el-input>
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                  v-model="loginForm.password"
                  placeholder="密码"
                  type="password"
                  show-password
              ></el-input>
            </el-form-item>

            <el-button type="primary" :loading="loading" class="login-button" @click="isLogin ? handleLogin() : handleRegister()">{{ isLogin ? '登录' : '注册' }}</el-button>
            <div>
              <el-button plain class="toggle-button" @click="toggleForm">{{ isLogin ? '没有账户？注册' : '已有账户？登录' }}</el-button>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { http } from '@/api'

export default {
  name: 'Login',
  data() {
    return {
      isLogin: true,
      loading: false,
      loginForm: {
        phone: '',
        password: ''
      },
      registerForm: {
        nickname: '',
        phone: '',
        password: ''
      },
      rules: {
        nickname: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async handleLogin() {
      try {
        this.loading = true
        const valid = await this.$refs.loginFormRef.validate()
        if (valid) {
          const res = await http.user.login(this.loginForm)
          this.$store.commit('user/SET_TOKEN', res.data.token)
          this.$store.commit('user/SET_USERINFO', res.data)
          this.$message.success('登录成功')
          setTimeout(() => {
            this.loading = false
            // 跳转到首页
            this.$router.push('/')
          },500)
        }
      } catch (error) {
        this.loading = false
        console.error('Login error:', error)
      }
    },
    async handleRegister() {
      try {
        const valid = await this.$refs.registerFormRef.validate()
        if (valid) {
          const res = await http.user.register(this.registerForm)
          this.$store.commit('user/SET_TOKEN', res.data.token)
          this.$store.commit('user/SET_USERINFO', res.data)
          this.$message.success('注册成功，登录中...')
          setTimeout(() => {
            this.loading = false
            // 跳转到首页
            this.$router.push('/')
          },500)
        }
      } catch (error) {
        console.error('Register error:', error)
        this.$message.error('注册失败，请稍后重试')
      }
    },
    toggleForm() {
      this.isLogin = !this.isLogin;
    }
  }
}
</script>

<style scoped lang="scss">
.login-container {
  min-height: 100vh;
  width: 100vw;
  background: #1C1C28;
  background-image: linear-gradient(to bottom right, rgba(116, 83, 218, 0.1), rgba(116, 83, 218, 0));
  display: flex;
  flex-direction: column;
}

.nav-bar {
  height: 80px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 40px;
  background: #fff;
}

.nav-left {
  display: flex;
  align-items: center;
  gap: 20px;

  .logo {
    img {
      height: 100px;
    }
  }
}

.content {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("~@/assets/images/login-bg.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

.login-box {
  margin-top: -40px;
  width: 400px;
  padding: 40px;
  background: #FFFFFF;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.05);
}

.login-form {
  h2 {
    text-align: left;
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 8px;
    color: #1C1C28;
  }

  .subtitle {
    text-align: left;
    color: #666;
    font-size: 14px;
    margin-bottom: 32px;
  }
}

.login-button {
  width: 100%;
  height: 40px;
  margin-top: 24px;
}

.toggle-button {
  width: 100%;
  margin-top: 16px;
  color: #7453DA;
}

.other-login {
  margin-top: 32px;
  text-align: center;

  .other-text {
    color: #7453DA;
    font-size: 14px;
    cursor: pointer;

    &:hover {
      color: darken(#7453DA, 10%);
    }
  }
}
</style>
